/*
 * @Author: 葛文杰
 * @Date: 2021/1/29 15:46
 * @Description: egg proxy 数据源字段
 */
const { IS_NOT_DELETE, BASE_WHERE_CONDITION_SQL } = require('../util/common');

const TABLE_NAME = 'data_source_field';

const TABLE_FIELD = 'id,fieldUseType,fieldName,fieldAnotherName,fieldType,fieldLength,dataSourceConfigId';

module.exports = {

    /**
     * 获取数据源字段列表
     */
    async getFieldListByDataSourceConfigId(conn, { dataSourceConfigId, pageNo, pageSize }) {
        let start = (pageNo - 1) * pageSize;
        let selectSql = `select ${TABLE_FIELD} from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        conditionSql = `${conditionSql}`;
        if (dataSourceConfigId) {
            conditionSql = `${conditionSql} and dataSourceConfigId = ?`;
            params.push(dataSourceConfigId);
        }
        let sql = `${selectSql} ${conditionSql} limit ?,?`;
        params = [ ...params, start, pageSize ];
        let dataSourceFieldList = await conn.query(sql, params);
        return dataSourceFieldList;
    },


    /**
     * 获取数据源字段总数
     * @param conn
     * @param dataSourceConfigId
     * @return {Promise<any>}
     */
    async getFieldTotalByDataSourceConfigId(conn, { dataSourceConfigId }) {
        let selectSql = `select count(1) as count from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [ IS_NOT_DELETE ];
        if (dataSourceConfigId) {
            conditionSql = `${conditionSql} and dataSourceConfigId = ?`;
            params.push(dataSourceConfigId);
        }
        let sql = `${selectSql} ${conditionSql}`;
        let [ total ] = await conn.query(sql, params);
        let { count } = total;
        return count;
    },

    /**
     * 获取字段信息
     * @param conn
     * @param dataSourceFieldId 数据源字段Id
     * @return {Promise<any>}
     */
    async getFieldInfoById(conn, dataSourceFieldId) {
        let selectSql = `select ${TABLE_FIELD} from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        if (dataSourceFieldId) {
            conditionSql = `${conditionSql} and id = ?`;
            params.push(dataSourceFieldId);
        }
        let sql = `${selectSql} ${conditionSql}`;
        let result = await conn.query(sql, params);
        return result;
    },

    /**
     * 获取显示字段数组
     * @return {Promise<void>}
     */
    async getShowFieldArrayByType(conn, { dataSourceFieldId, fieldUseType }) {
        let selectSql = `select id as value,fieldName as label from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        if (dataSourceFieldId) {
            conditionSql = `${conditionSql} and dataSourceFieldId = ?`;
            params.push(dataSourceFieldId);
        }
        if (fieldUseType) {
            conditionSql = `${conditionSql} and fieldUseType = ?`;
            params.push(fieldUseType);
        }
        let sql = `${selectSql} ${conditionSql}`;
        let result = await conn.query(sql, params);
        return result;
    },

    /**
     * 保存数据源字段信息
     */
    async insertDataSourceFieldInfo(conn, dataSourceFieldInfo) {
        let result = await conn.insert(TABLE_NAME, dataSourceFieldInfo);  // 第一步操作
        return result;
    },

    /**
     * 更新数据源字段
     * @param conn
     * @param condition
     * @return {Promise<void>}
     */
    async updateDataSourceFieldInfoById(conn, condition) {
        let result = await conn.update(TABLE_NAME, condition);  // 第一步操作
        return result;
    },

    /**
     * 批量更新临时数据源id为真实id
     * @param conn
     * @param value
     * @param condition
     * @return {Promise<void>}
     */
    async updateDataSourceConfigTempIdById(conn, value, condition) {
        let result = await conn.update(TABLE_NAME, value, condition);  // 第一步操作
        return result;
    },

    /**
     * 删除数据源字段信息
     * @param conn
     * @param condition 删除条件
     * @return {Promise<void>}
     */
    async deleteFieldInfoById(conn, condition) {
        let result = await conn.delete(TABLE_NAME, condition);  // 第一步操作
        return result;
    }
};
